<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 添加分类按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCate">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :show-index="true"
        :expand-type="false"
        stripe
        border
        class="treeTable"
      >
        <!-- 是否有效 -->
        <template slot="isOk" slot-scope="scope">
          <i class="el-icon-success iSuccess" v-if="!scope.row.cat_deleted"></i>
          <i class="el-icon-error iError" v-else></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="option">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateDialogClose"
    >
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name" placeholder="请输入分类名称"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="selectData"
            :options="parentCateList"
            :props="cascaderProps"
            @change="cascaderChange"
            :clearable="true"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addCateConfirm">确 定</el-button>
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as allGoodsFunc from '@/network/goods'
export default {
  name: 'Cate',
  data() {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      cateList: [],
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'option'
        }
      ],
      addCateDialogVisible: false,
      addCateForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentCateList: [],
      selectData: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true
      }
    }
  },
  created() {
    this.getAllCateData(this.queryInfo)
  },
  methods: {
    // 获取所有分类数据
    async getAllCateData(config) {
      const { data, meta } = await allGoodsFunc.getCateData(config)
      if (meta.status !== 200) {
        return this.$message.error(meta.msg)
      }
      this.cateList = data.result
      this.total = data.total
    },
    // pageSize 改变时会触发
    handleSizeChange(newPageSize) {
      this.queryInfo.pagesize = newPageSize
      this.getAllCateData(this.queryInfo)
    },
    // currentPage 改变时会触发
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getAllCateData(this.queryInfo)
    },
    addCate() {
      this.getParentCateList()
      this.addCateDialogVisible = true
    },
    async getParentCateList() {
      const { data, meta } = await allGoodsFunc.getParentCateData()
      if (meta.status !== 200) {
        return this.$message.error(meta.msg)
      }
      this.parentCateList = data
    },
    cascaderChange() {
      if (this.selectData.length > 0) {
        this.addCateForm.cat_level = this.selectData.length
        this.addCateForm.cat_pid = this.selectData[this.selectData.length - 1]
      } else {
        this.addCateForm.cat_level = 0
        this.addCateForm.cat_pid = 0
      }
    },
    addCateConfirm() {
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) {
          return false
        } else {
          const { meta } = await allGoodsFunc.addCate(this.addCateForm)
          if (meta.status !== 201) {
            return this.$message.error(meta.msg)
          } else {
            this.$message.success(meta.msg)
            this.getAllCateData(this.queryInfo)
            this.addCateDialogVisible = false
          }
        }
      })
    },
    addCateDialogClose() {
      this.$refs.addCateFormRef.resetFields()
      this.selectData = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    }
  }
}
</script>

<style lang="less" scoped>
.iSuccess {
  color: lightgreen;
}
.iError {
  color: red;
}
.treeTable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
